package com.wss.module.wan.ui.series;

import cn.bingoogolapple.refreshlayout.BgarefreshLayout;
import com.orhanobut.logger.Logger;
import com.wss.common.base.BaseRefreshListAbility;
import com.wss.common.base.adapter.listener.OnListItemClickListener;
import com.wss.common.constants.Dic;
import com.wss.common.manage.AbilityToAbility;
import com.wss.common.utils.ToastUtils;
import com.wss.module.wan.bean.Article;
import com.wss.module.wan.bean.Classification;
import com.wss.module.wan.ui.main.adapter.ArticleAdapter;
import com.wss.module.wan.ui.series.mvp.SeriesArticlePresenter;
import com.wss.module.wan.ui.series.mvp.contract.SeriesArticleContract;
import ohos.aafwk.content.Intent;
import ohos.agp.components.BaseItemProvider;
import java.util.ArrayList;
import java.util.List;

public class SeriesArticleAbility extends BaseRefreshListAbility<SeriesArticlePresenter> implements
        SeriesArticleContract.View {

    private List<Article> articleList = new ArrayList<>();

    private int page;

    private Classification children;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
    }

    @Override
    protected void initView() {
        super.initView();
        children = getIntent().getSerializableParam(Dic.CLASSIFICATION_CHILD);
        if (children == null) {
            children = new Classification();
        }
        setCenterText(children.getName());
        getPresenter().getArticle();
    }

    @Override
    protected SeriesArticlePresenter createPresenter() {
        return new SeriesArticlePresenter();
    }

    @Override
    protected BgarefreshLayout.BgarefreshLayoutDelegate createRefreshListener() {
        return new BgarefreshLayout.BgarefreshLayoutDelegate() {
            @Override
            public void onBgarefreshLayoutBeginRefreshing(BgarefreshLayout bgarefreshLayout) {
                resetPage();
            }

            @Override
            public boolean onBgarefreshLayoutBeginLoadingMore(BgarefreshLayout bgarefreshLayout) {
                getPresenter().getArticle();
                return true;
            }
        };
    }

    @Override
    protected BaseItemProvider createAdapter() {
        return new ArticleAdapter(getContext(), articleList, new OnListItemClickListener<Article>() {
            @Override
            public void onItemClick(Article data, int position) {
                AbilityToAbility.toWebView(context, data.getLink());
            }
        });
    }

    @Override
    public String getSeriesId() {
        return String.valueOf(children.getId());
    }

    @Override
    public int getPage() {
        return page;
    }

    @Override
    public void refreshArticleList(List<Article> articles) {

//        int position = this.articleList.size();
        this.articleList.addAll(articles);
        getAdapter().notifyDataChanged();
//        if (page > 0) {
//            getRecyclerView().scrollTo(position + 3);
//        }
        page++;
    }

    @Override
    public void onError(Object tag, String errorMsg) {
        super.onError(tag, errorMsg);
        if (page == 0) {
            showErrorView(errorMsg);
        } else {
            ToastUtils.show(errorMsg);
        }
    }

    @Override
    public void onEmpty(Object tag) {
        super.onEmpty(tag);
        if (page == 0) {
            showEmptyView();
        } else {
            ToastUtils.show("暂无更多数据");
        }
    }

    @Override
    protected void onRefreshRetry() {
        super.onRefreshRetry();
        resetPage();
    }

    private void resetPage() {
        page = 0;
        articleList.clear();
        getPresenter().getArticle();
    }
}
